//
//  MXMineItemCollectionVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/20.
//  Copyright (c) 2015年 霄 马. All rights reserved.
//

#import "MXMineItemCollectionVC.h"
#import "MXFindContentCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "DataBaseFMDB.h"
#import "MXDataBaseItemModel.h"
#import "MXHomeWebViewVC.h"
#import "MXMineSingLabel.h"
#import "MXMineItemDetailTableViewCell.h"
@interface MXMineItemCollectionVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *itemArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *linkArray;
@property (nonatomic, retain) NSString *tempTitleString;
@property (nonatomic, retain) NSMutableArray *titleArray;
@end

@implementation MXMineItemCollectionVC
- (void)dealloc
{
    [_titleArray release];
    [_tempTitleString release];
    [_tableView release];
    [_linkArray release];
    [_itemArray release];
    [super dealloc];
}


-(void)viewWillAppear:(BOOL)animated
{
    
    DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];

    // 创建数据库
    [dbManger createItemDataBase];
    
    // 创建表
    [dbManger creatItemDaseTable];
        
    self.itemArray = [dbManger selectItemData];

    
    
    MXMineSingLabel *singLabel = [MXMineSingLabel singhLabel];
    
    
    [self.view addSubview:singLabel];
    
    if (self.itemArray.count == 0) {
        singLabel.hidden = NO;
    } else {
        singLabel.hidden = YES;
    }
    
    [self.tableView reloadData];
    
//    self.tabBarController.tabBar.hidden = YES;

}

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.tabBarController.tabBar.hidden = YES;

    self.title = @"单品收藏";
    
    self.itemArray = [NSMutableArray array];
    self.linkArray = [NSMutableArray array];
    self.titleArray = [NSMutableArray array];
    
    DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];
    
    // 创建数据库
    [dbManger createItemDataBase];
    
    // 创建表
    [dbManger creatItemDaseTable];
    
    self.itemArray = [dbManger selectItemData];

        
    [self creatTableView];
    
    
//    self.navigationItem.rightBarButtonItem = self.editButtonItem;

}

#pragma mark - FindTabBar设置TableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - HEIGHTPRO * 64) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
}
/************************* TableView Delegate **************************/

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MXMineItemDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"resue"];
    if (!cell) {
        cell = [[MXMineItemDetailTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"reuse"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:[self.itemArray[indexPath.row] imagePath]] placeholderImage:[UIImage imageNamed:@"plaseholder"]];
    
    cell.titleLabel.text = [self.itemArray[indexPath.row] detail];
    
    cell.priceLabel.text = [NSString stringWithFormat:@"¥ %@", [self.itemArray[indexPath.row] price]];
    
    [self.titleArray addObject:[self.itemArray[indexPath.row] detail]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return HEIGHTPRO * 460;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeWebViewVC *webViewVC = [[[MXHomeWebViewVC alloc] init] autorelease];
    
    DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];
    
    // 创建数据库
    [dbManger createItemDataBase];
    
    // 创建表
    [dbManger creatItemDaseTable];
    
    
    NSMutableArray *array = [dbManger selectItemData];
    
    
    for (MXDataBaseItemModel *model in array) {
        [self.linkArray addObject:model.ItemID];
    }
    
    webViewVC.requestString = self.linkArray[indexPath.row];
    
    [self presentViewController:webViewVC animated:YES completion:^{
        
    }];

}

#pragma mark - tableView编辑方法
// 2.设置编辑方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    // 调用父类的方法
    [super setEditing:editing animated:animated];
    // 利用系统提供的编辑按钮 开启/关闭编辑状态
    [self.tableView setEditing:!editing animated:animated];
}
// 3.是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 默认 全部都可以编辑
    return YES;
}
// 4.设置cell的编辑样式(编辑类型)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
        return UITableViewCellEditingStyleDelete;
}

// 5.确认删除信息
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
// 6.通过编辑样式 对tableView进行 删除/插入 操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 通过editingStyle判断 执行 删除/插入
    
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 先删除数据源中的数据
        [self.itemArray removeObjectAtIndex:indexPath.row];
        
        
        DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];
        
        // 创建数据库
        [dbManger createItemDataBase];
        
        // 创建表
//        [dbManger creatItemDaseTable];
        
//        [dbManger deleteItemData:self.titleArray[indexPath.row]];
        
//        MXDataBaseItemModel *model = self.itemArray[indexPath.row];
        
        [dbManger selectItemData];
        
        [dbManger deleteItemData:self.titleArray[indexPath.row]];
        
        [self.titleArray removeObjectAtIndex:indexPath.row];
        MXMineSingLabel *singLabel = [MXMineSingLabel singhLabel];
        
        
        [self.view addSubview:singLabel];
        
        if (self.itemArray.count == 0) {
            singLabel.hidden = NO;
        } else {
            singLabel.hidden = YES;
        }
        
//        [self.tableView reloadData];

        
        
        
        // 保存删除的位置信息(使用数组)
        NSArray *indexArr = @[indexPath];
        // 操作视图 让视图和数据源匹配
        // 参数1: 位置信息
        // 参数2: 删除动画
        [self.tableView deleteRowsAtIndexPaths:indexArr withRowAnimation:UITableViewRowAnimationRight];
    }
}



/*
//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.itemArray.count;
}

//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];
    cell.layer.masksToBounds = YES;

    [cell getItemInfo:self.itemArray[indexPath.row]];
    
    return cell;
    
}




//collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXHomeWebViewVC *webViewVC = [[[MXHomeWebViewVC alloc] init] autorelease];
    
    DataBaseFMDB *dbManger = [[[DataBaseFMDB alloc] init] autorelease];
    
    // 创建数据库
    [dbManger createItemDataBase];
    
    // 创建表
    [dbManger creatItemDaseTable];
    

    NSMutableArray *array = [dbManger selectItemData];
    
    
    for (MXDataBaseItemModel *model in array) {
        [self.linkArray addObject:model.ItemID];
    }
    
    webViewVC.requestString = self.linkArray[indexPath.row];
    
    NSLog(@"%@", webViewVC.requestString);
    
    [self presentViewController:webViewVC animated:YES completion:^{
        
    }];
}
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
